package nacos

import (
	"gitee.com/wang-xiangyu20000112/vblog/logger"
	"github.com/nacos-group/nacos-sdk-go/v2/clients"
	"github.com/nacos-group/nacos-sdk-go/v2/common/constant"
	"github.com/nacos-group/nacos-sdk-go/v2/vo"
	"go.uber.org/zap"
)

// 初始化nacos配置
func InitNacosConfig() {
	// 创建客户端配置
	clientConfig := constant.NewClientConfig(
		constant.WithNamespaceId("cba581dc-9d3b-4e65-aec4-5e957d1224e6"),
		constant.WithTimeoutMs(5000),
		constant.WithNotLoadCacheAtStart(true),
		constant.WithLogDir("E:\\WorkSpace\\vblog\\log"),
		constant.WithCacheDir("E:\\WorkSpace\\vblog\\cache"),
		constant.WithLogLevel("debug"),
		constant.WithUsername("nacos"),
		constant.WithPassword("156832zxc@"),
	)

	// 服务端配置
	serverConfigs := []constant.ServerConfig{
		*constant.NewServerConfig(
			"192.168.1.115",
			8848,
			constant.WithContextPath("/nacos"),
			constant.WithScheme("http"),
		),
	}

	// 创建服务发现客户端
	nameClient, nameClientErr := clients.NewNamingClient(
		vo.NacosClientParam{
			ClientConfig:  clientConfig,
			ServerConfigs: serverConfigs,
		},
	)

	if nameClientErr != nil {
		logger.ZapLogger.Error("nacos client error", zap.Error(nameClientErr))
	}

	// 创建动态配置客户端
	// configClient, configClientERR := clients.NewConfigClient(
	// 	vo.NacosClientParam{
	// 		ClientConfig: clientConfig,
	// 		ServerConfigs: serverConfigs,
	// 	},
	// )

	// if configClientERR != nil {
	// 	logger.ZapLogger.Error("nacos client error", zap.Error(configClientERR))
	// }

	// 确认服务实例是否正确注册
	ok, err := nameClient.RegisterInstance(vo.RegisterInstanceParam{
		Ip:          "192.168.1.150",
		Port:        10080,
		Weight:      10,
		Enable:      true,
		Healthy:     true,
		ServiceName: "vblog",
		Ephemeral:   true,
	})
	if err != nil {
		logger.ZapLogger.Error("nacos register error", zap.Error(err))
	}
	if !ok {
		logger.ZapLogger.Info("注册本服务发生错误")
		logger.ZapLogger.Error("注册本服务发生错误")
	} else {
		logger.ZapLogger.Info("服务实例注册成功")
	}
}
